package xyz.dava.sync.fm.controller;

import cn.hutool.core.io.resource.ClassPathResource;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import xyz.dava.sync.fm.util.BomUtil;
import xyz.dava.sync.fm.vo.InputParams;
import xyz.dava.sync.fm.vo.SapParam;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

@RequestMapping("/sap")
@RestController
public class SapController {
    public static final String TOKEN = "d67e94c60cbb4406a85fc04a475f92ff";

    private static final String BASE_URL = "http://webqas08.shanghai-electric.com/dce-api/sjram-service/api/do/SFFILEMANAGE/D11/114/";

    private static final String ZPP_BOM_SINGLE_EXP_ALL = BASE_URL + "ZPP_BOM_SINGLE_EXP_ALL";
    private static final String ZSCMS_DOC_READ_PLM = BASE_URL + "ZSCMS_DOC_READ_PLM";
    private static final String BAPI_DOCUMENT_GETLIST2 = BASE_URL + "BAPI_DOCUMENT_GETLIST2";

    private final RestTemplate restTemplate = new RestTemplate();

    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping("/ZPP_BOM_SINGLE_EXP_ALL")
    public Object ZPP_BOM_SINGLE_EXP_ALL(String token, @RequestBody Map<String, Object> data) {
        SapParam param = new SapParam(new InputParams(data, new HashMap<>()), token);
        System.out.println(JSONUtil.toJsonPrettyStr(param));
        return restTemplate.postForEntity(ZPP_BOM_SINGLE_EXP_ALL, param, HashMap.class).getBody();
    }

    @RequestMapping("/ZSCMS_DOC_READ_PLM")
    public Object ZSCMS_DOC_READ_PLM(String token, @RequestBody Map<String, Object> data) {
        return restTemplate.postForEntity(ZSCMS_DOC_READ_PLM, new SapParam(new InputParams(data, new HashMap<>()), token), HashMap.class).getBody();
    }

    @RequestMapping("/BAPI_DOCUMENT_GETLIST2")
    public Object BAPI_DOCUMENT_GETLIST2(String token, @RequestBody Map<String, Object> data) {
        return restTemplate.postForEntity(BAPI_DOCUMENT_GETLIST2, new SapParam(new InputParams(data, new HashMap<>()), token), HashMap.class).getBody();
    }

    @RequestMapping("/calculateParam")
    public Object calculate(String token, Long time, @RequestBody Map<String, Object> data) {
        time = time == null ? (System.currentTimeMillis() / 1000) : time;
        Map<String, Object> result = new HashMap<>();
        result.put("signature", BomUtil.getSignature(token, data, time));
        result.put("signature_created", time);
        result.put("input_params", data);
        return result;
    }

    @RequestMapping("/calculateParam2")
    public Object calculate2(String token, Long time, @RequestBody Map<String, Object> data) {
        return JSONUtil.toJsonPrettyStr(new SapParam(new InputParams(data, null), token, time));
    }

    @RequestMapping("/calculateParam3")
    public Object calculate3(@RequestBody LinkedHashMap<String, Object> data) {
        return data;
    }

    @RequestMapping("/all")
    public Object all(@RequestBody LinkedHashMap<String, Object> data) throws IOException {
        return objectMapper.readValue(new ClassPathResource("all.json").getFile(),HashMap.class);
    }

    @RequestMapping("/list")
    public Object list(@RequestBody LinkedHashMap<String, Object> data) throws IOException {
        return objectMapper.readValue(new ClassPathResource("list.json").getFile(),HashMap.class);
    }

    @RequestMapping("/download")
    public Object download(@RequestBody LinkedHashMap<String, Object> data) throws IOException {
        return objectMapper.readValue(new ClassPathResource("download.json").getFile(),HashMap.class);
    }
    @RequestMapping("/detail")
    public Object detail(@RequestBody LinkedHashMap<String, Object> data) throws IOException {
        return objectMapper.readValue(new ClassPathResource("detail.json").getFile(),HashMap.class);
    }
}
